package leetcode.N1_N100;

/**
 * @author xiwai [hww282306@alibaba-inc.com]
 * @version 1.0
 * @date 2020/10/9 8:52 下午
 */
public class N27 {

    public static void main(String[] args) {

        int[] nums = {3,3};
        int val = 3;
        removeElement(nums, val);
    }

    public static int removeElement(int[] nums, int val) {
        int length = nums.length;
        if (length == 0) {
            return 0;
        }
        int tailIndex = length - 1;
        for (int i = 0; i < length; i++) {
            if (i >= tailIndex) {
                return nums[tailIndex] == val ? tailIndex : tailIndex + 1;
            }
            int num = nums[i];
            if (num == val) {
                for (int j = tailIndex; j >= 0 ; j--) {
                    if (j <= i) {
                        return i;
                    }
                    if (nums[j] == val) {
                        continue;
                    }
                    nums[i] = nums[j];
                    tailIndex = j - 1;
                    break;
                }
            }
        }
        return tailIndex + 1;
    }

}
